<?php

class Titik_penjualan extends CI_Controller {
	
	function Titik_penjualan()
	{
		parent::__construct();
		$this->load->model('titik_penjualan_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
		$this->load->model('satuan_model', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		
		
		
		// Load default view
		$data['title'] = "Titik Penjualan";
		$data['nama_user'] = 'Admin';
		$data['main_view'] = 'titik_penjualan/titik_penjualan_view';
		
		$data['all_titik_penjualan'] = $this->titik_penjualan_model->get_all_titik_penjualan();
		
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['all_kota'] = $this->lokasi_model->get_all_kota();
		$data['all_satuan'] = $this->satuan_model->get_all_satuan();
		$all_titik_penjualan = $this->titik_penjualan_model->get_all_titik_penjualan();
		if($all_titik_penjualan == null)
			$data['all_titik_penjualan'] = null;
		else {
			$data['all_titik_penjualan'] = $all_titik_penjualan->result_array();
			foreach($data['all_titik_penjualan'] as $titik){
				$titik['DATA_PENJUALAN'] = $this->titik_penjualan_model->get_latest_stok($titik['ID_TITIK_PASOK']);
				if($titik['DATA_PENJUALAN'] == null)
					$titik['DATA_PENJUALAN'] = null;
				else 
					$titik['DATA_PENJUALAN'] = $titik['DATA_PENJUALAN']->result_array();
			}
		}
		
		
		$all_kordinat = $this->titik_penjualan_model->get_all_kordinat();
		if($all_kordinat == null)
			$data['all_kordinat'] = null;
		else
			$data['all_kordinat'] = $all_kordinat->result_array();
		
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function add_titik_penjualan(){
	
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$id_titik_pasok = $this->titik_penjualan_model->insert_titik_pasok($arr);
		
		$arr2 = array(
			'ID_TITIK_PASOK' => $id_titik_pasok,
			'PERMINTAAN_NORMAL' => $this->input->post('penjualan_normal'),
		);
		
		$this->titik_penjualan_model->insert_titik_penjualan($arr2);
		
	}
	
	function update_titik_penjualan(){
		$id = $this->input->post('id_titik_penjualan');
		
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		if(count($tanggal_arr) > 1) $tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr1 = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$arr2 = array(
			'PERMINTAAN_NORMAL' => $this->input->post('penjualan_normal')
		);
		
		
		$this->titik_penjualan_model->update_titik_penjualan($id, $arr1, $arr2);
		
	}
	
	function delete_titik_penjualan(){
		$id_pasok = $this->input->post('id_titik_pasok');
		$this->titik_penjualan_model->delete_titik_penjualan($id_pasok);
	}
	
	function update_role(){
		$id = $this->input->post('id_role');
		$data['role_res'] = $this->role_model->get_role($id);
		$this->load->view('role/role_edit', $data);
		
	}
	
	function submit_role(){
		$id = $this->input->post('id_role');
		$arr = array(
			'NAMA_ROLE' => $this->input->post('nama_role')
		);
		
		$this->role_model->update_role($id, $arr);
		
		redirect('role');
	}
	
	
	
}